import { ref, watch } from 'vue';
import { meetingDetailApi } from '/@/api/expertDatabase/extract';

import { useRoute } from 'vue-router';
import { ElLoading } from 'element-plus';
import { getRouterParams } from '/@/utils/hyFunc';

export function useDetail() {
	const route = useRoute();
	const routerParams: any = ref('');
	const basicData = ref({
		meetingName: '',
		reviewTime: '',
		estimatedReviewTime: undefined,
		arrivalTime: '',
		contactPerson: '',
		contactInformation: '',
		reviewType: '',
		appCategory: '',
		reviewAddress: '',
		noticeContent: '',
		appCategoryList: '',
		id: '',
	});
	const reviewAppData = ref([]);
	const waysData = ref({
		extractionModeList: [],
	});
	const expertWayData = ref({
		expertExtractionTypeList: [],
		expertManagerCount: undefined,
		expertTechnologyCount: undefined,
		expertIndustryFields: [],
		expertProfessionalFields: [],
		expertAvoidanceUnits: [],
		expertSpecifiedCause: '',
	});

	const expertAvoidanceData = ref([]);
	const expertSpecifiedData = ref([]);
	const institutionWayData = ref({
		institutionSecret: '',
		institutionCount: undefined,
	});
	const institutionAvoidanceData = ref([]);
	const extractResultData = ref({
		extractStatus: '',
		extractResult: '',
		extractResultRemark: '',
		extractTime: '',
		extractStatusName: '',
	});
	const expertListData = ref([]);
	const institutionListData = ref([]);
	const getDetailData = (id: string) => {
		const loading = ElLoading.service({
			lock: true,
			text: '获取数据中...',
		});
		meetingDetailApi({ id })
			.then((res: any) => {
				basicData.value = mergeInfoData(basicData.value, res.baseInfo);
				reviewAppData.value = res.reviewAppList || [];
				waysData.value = res.extractionMode.extractionModeList || [];
				expertAvoidanceData.value = res.extractionMode.expertAvoidanceList || [];
				expertSpecifiedData.value = res.extractionMode.expertSpecifiedList || [];
				institutionAvoidanceData.value = res.extractionMode.institutionAvoidanceList || [];
				extractResultData.value = res.extractionResult;
				expertListData.value = res.expertList || [];
				institutionListData.value = res.institutionList || [];
				expertWayData.value = mergeInfoData(
					{ ...expertWayData.value, expertIndustryFieldsName: [], expertProfessionalFieldsName: [] },
					res.extractionMode
				);
				institutionWayData.value = mergeInfoData(institutionWayData.value, res.extractionMode);
			})
			.finally(() => {
				loading.close();
			});
	};
	//设置标题
	const title = ref('');
	const setTtile = (type: any) => {
		if (type === 'add') {
			title.value = '新增';
		}
		if (type === 'edit') {
			title.value = '编辑';
		}
		if (type === 'submit') {
			title.value = '提交';
		}
		if (type === 'view') {
			title.value = '详情';
		}
		if (type === 'approve') {
			title.value = '审核';
		}
	};
	// 合并两个对象，并处理特殊字段
	const mergeInfoData = (target: any, source: any) => {
		let result: any = {};
		Object.keys(target).forEach((key) => {
			result[key] = source[key];
		});
		return result;
	};
	watch(
		[() => route.query],
		() => {
			routerParams.value = getRouterParams();
			if (routerParams.value.operateType !== 'add') {
				getDetailData(routerParams.value.id);
			}
			setTtile(routerParams.value?.operateType);
		},
		{ immediate: true, deep: true }
	);
	return {
		getDetailData,
		title,
		routerParams,
		basicData,
		reviewAppData,
		waysData,
		expertWayData,
		institutionWayData,
		expertAvoidanceData,
		expertSpecifiedData,
		institutionAvoidanceData,
		extractResultData,
		expertListData,
		institutionListData,
	};
}
